package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVD
{
    // instance variables - replace the example below with your own
    private ArrayList<Copy> copylist;
    private int id;
    private String title;
    private String publisher;
    private String releasedate;
    private int serieno;

    public DVD(int id, String title, String publisher, String releasedate)
    {    
        this.id = id;
        this.title = title;
        this.publisher = publisher;
        this.releasedate = releasedate;
        copylist = new ArrayList<Copy>();
        serieno = 0;
    }

    //get methods    
    public int getID()
    {
        return id;
    }

    public String getTitle()
    {
        return title;
    }

    public String getPublisher()
    {
        return publisher;
    }

    public String getReleasedate()
    {
        return releasedate;
    }

    //set methods
    public void setID(int id)
    {
        this.id = id;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setPublisher(String publisher)
    {
        this.publisher = publisher;
    }

    public void setReleasedate(String releasedate)
    {
        this.releasedate = releasedate;
    }    

    public void createCopy(String acdate, int acprice)
    {
        serieno++;
        Copy copy = new Copy(serieno, acdate, acprice);
        copylist.add(copy);
    }

    public Copy findCopy(int no)
    {
        Copy copy = null;
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < copylist.size() && !found)
        {
            copy = copylist.get(index);

            if(copy.getSerieno() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            return copylist.get(rememberIndex);
        }
        else
        {
            return null;
        }
    }

    //update dvd in ArrayList<DVD>
    public void UpdateCopy(int no, String acdate, int acprice)
    {
        Copy copy = null;
        int index =  0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < copylist.size() && !found)
        {
            copy = copylist.get(index);

            if(copy.getSerieno() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            copy = copylist.get(rememberIndex);
            copy.setAcdate(acdate);
            copy.setAcprice(acprice);
            System.out.println("The copy number "+no+" has been updated");
        }
        else
        {
            System.out.println("The copy number "+no+" dosen't exist");
        }

    }

    public void deleteCopy(int no)
    {
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;
        Copy copy = null;

        while(index < copylist.size() && !found)
        {
            copy = copylist.get(index);

            if(copy.getSerieno() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;

        }
        if(found)
        {
            System.out.println("The copy with series number "+no+" has been removed.");
            copylist.remove(rememberIndex);
        }
        else
        {
            System.out.println("The copy with series number "+no+" dosen't exist");
        }

    }

    public void printDetailDVD()
    {

        if(copylist != null)
        {
            System.out.println();
            System.out.println("# DVD: "+ getTitle());
            System.out.println("#----------------------------------");
            System.out.println("# ID            :"+ id);
            System.out.println("# title         :"+ getTitle());
            System.out.println("# publisher     :"+ getPublisher());
            System.out.println("# release date  :"+ getReleasedate()); 
            System.out.println();
            System.out.println("There are "+ getSizeOfCopy()+" copies of the DVD "+getTitle());
        }
        else
        {
            System.out.println("No DVD created! ");
        }

    }

    public void printAllCopy()
    {

        if(copylist != null)
        {
            System.out.println();
            System.out.println("# DVD: "+ getTitle());
            System.out.println("#----------------------------------");
            System.out.println("# ID            :"+ id);
            System.out.println("# title         :"+ getTitle());
            System.out.println("# publisher     :"+ getPublisher());
            System.out.println("# release date  :"+ getReleasedate());
            System.out.println();
            System.out.println("There are "+ getSizeOfCopy()+" copies of the DVD "+getTitle());

            if(copylist !=null)
            {
                for(Copy filename : copylist)
                {                    
                    filename.printDetail();
                }
            }
            else 
            {
                System.out.println("This DVD have no copies");
            }           

        }
        else
        {
            System.out.println("No DVD created! ");
        }

    }

    public int getSizeOfCopy()
    {
        return copylist.size();
    }
    
    public int getSerieNo()
    {
        return serieno;
    }
    
}
